#include<stdio.h>

//学生结构体
struct Student{
	char name[15]; //姓名
	char gender[8]; //性别
	int age; //年龄
	int class; //班级
	int number; //学号
	int score; //成绩
	
};

//学生结构体数组
struct Student studentList[100];

//记录当前学生个数
int studentCount = 0;

//创建菜单
void menu()
{
	printf("===============主菜单=================\n");
	printf("1.添加学生信息\n");
	printf("2.删除学生信息\n");
	printf("3.修改学生信息\n");
	printf("4.查询学生信息\n");
	printf("5.排序学生信息\n");
	printf("6.保存\n");
	printf("0.退出\n");
	printf("======================================\n");
}

//添加学生信息
void addStudent()
{	//设置一个死循环
	char selectYNFlag = 'y';
	while(selectYNFlag == 'Y'||selectYNFlag == 'y')
	{
		//输入学生信息
		printf("\n请输入姓名：");
		scanf("%s",studentList[studentCount].name);
		printf("请输入性别：");
		scanf("%s",studentList[studentCount].gender);
		printf("请输入年龄：");
		scanf("%d",&studentList[studentCount].age);
		printf("请输入班级：");
		scanf("%d",&studentList[studentCount].class);
		printf("请输入学号：");
		scanf("%d",&studentList[studentCount].number);
		printf("请输入成绩：");
		scanf("%d",&studentList[studentCount].score);
		
		studentCount++;
		printf("是否继续添加?（继续添加输入“y”或“Y”，停止添加输入其余任意字母和数字)：");
		getchar();
		scanf("%c",&selectYNFlag);
	}
	printf("\n录入完成！\n\n",studentCount);
}

//删除学生信息
void deleteStudent()
{
	printf("请输入要删除的学生序号：");
	int indexDelete;
	scanf("%d",&indexDelete);
	int result = deleteStudentByIndex(indexDelete);
	if(result)
	{
		printf("\n删除成功！\n\n");
	}else{
		printf("\n删除失败！\n\n");
	}
}
int deleteStudentByIndex(int indexDelete)	//根据下表删除学生信息
{if(indexDelete<0||indexDelete>=studentCount)
	{
		return 0; //删除失败
	}	
	int i;
	for(i = indexDelete;i<studentCount-1;i++)
	{
		studentList[i]=studentList[i+1];
	}
	studentCount--;
	return 1; //删除成功
}
	
//修改学生信息
void modifyStudent()
{
	printf("请输入要修改学生的序号:");
	int indexModify = -1;
	scanf("%d",&indexModify);
	if(indexModify >=0 && indexModify < studentCount)
	{
		printf("请输入修改后学生姓名：");
		char name[15];
		scanf("%s",studentList[indexModify].name);
		printf("请输入修改后性别：");
		char gender[8];
		scanf("%s",studentList[indexModify].gender);
		printf("请输入修改后年龄：");
		int age;
		scanf("%d",&studentList[indexModify].age);
		printf("请输入修改后班级：");
		int class;
		scanf("%d",&studentList[indexModify].class);
		printf("请输入修改后学号：");
		int number;
		scanf("%d",&studentList[indexModify].number);
		printf("请输入修改后成绩：");
		int score;
		scanf("%d",&studentList[indexModify].score);
		
		printf("\n修改成功！\n\n");
		
	}else{
		printf("\n请输入正确的序号!\n\n");
	}
}

//查询学生信息
void findStudent(int all)
{
	int select = 0;
	char conCharArr[40];
	int conNum1,conNum2;
	
	if(all == 0) //选择
	{
		printf("\n0:全部\n");
		printf("1:按姓名查询\n");
		printf("2:按性别查询\n");
		printf("3:按成绩查询\n");
		printf("\n请输入选择:");
		scanf("%d",&select);
		if(select == 1||select ==2)
		{
			printf("请输入查询条件：");
			scanf("%s",conCharArr);
		}else if(select == 3)
		{
			printf("请输入查询范围m,n(用空格隔开)：");
			scanf("%d",&conNum1);
			scanf("%d",&conNum2);
		}
	}
	

	int i;
	int count = 0;
	printf("\n%-13s%-13s%-10s%-10s%-10s%-10s%-10s\n\n",
	"序号","姓名","性别","年龄","班级","学号","分数");
	for(i = 0;i < studentCount;i++)
	{
		if(
			select == 0||
			select == 1 && strstr(studentList[i].name,conCharArr)||
			select == 2 && strstr(studentList[i].gender,conCharArr)||
			select == 3 && studentList[i].score>=conNum1 && studentList[i].score<=conNum2
			
		){
			printf("%-13d%-13s%-10s%-10d%-10d%-10d%-10d\n\n",i,
			studentList[i] . name,
			studentList[i] . gender,
			studentList[i] . age,
			studentList[i] . class,
			studentList[i] . number,
			studentList[i] . score);
		count ++;
		}
	}
	printf("\n总人数：%d\n\n",count);
}

//排序学生信息
void sortStudent()
{
	int select,i,j;
	printf("1:按 姓名 排序\n");
	printf("2:按 性别 排序\n");
	printf("3:按 年龄 排序\n");
	printf("4:按 班级 排序\n");
	printf("5:按 学号 排序\n");
	printf("6:按 成绩 排序\n");
	printf("请输入选择：");
	scanf("%d",&select);
	for(i = studentCount-1;i>0;i--) //确定排序次数
	{
		for(j=0;j<i;j++) //确定单轮排序次数
		{
			if(
			1 == select && strcmp(studentList[j].name,studentList[j+1].name)>0 ||
			2 == select && strcmp(studentList[j].gender,studentList[j+1].gender)>0 ||
			3 == select && studentList[j].age > studentList[j+1].age ||
			4 == select && studentList[j].class > studentList[j+1].class||
			5 == select && studentList[j].number > studentList[j+1].number ||
			6 == select && studentList[j].score > studentList[j+1].score )
			{
				struct Student student = studentList[j];
				studentList[j] = studentList[j+1];
				studentList[j+1] = student;
			}
		}
	}
	findStudent(1);
	printf("\n排序完成！\n\n");
}

//写入学生信息
void writeStudent()
{
	FILE * fp = NULL;
	fp = fopen("Student.txt","w+");
	int i;
	for(i = 0;i < studentCount;i++)
	{
		fprintf(fp,"%s %s %d %d %d %d\n",studentList[i].name ,studentList[i].gender ,
		studentList[i].age ,studentList[i].class ,studentList[i].number ,studentList[i].score);
	}
	if(fp)
	fclose(fp);
}

//读取学生信息
void readStudent()
{
	FILE * fp = NULL;
	if(NULL == (fp = fopen("Student.txt","r")))
	{
		fclose(fp);
		return;
	}
	int i = 0;
	while(fscanf(fp,"%s%s%d%d%d%d\n",studentList[i].name ,studentList[i].gender ,
		&studentList[i].age ,&studentList[i].class ,&studentList[i].number ,&studentList[i].score)!=EOF)
		{
			i++;
		}
		studentCount = i;
		if(fp)
		fclose(fp);
}

void menu();
void addStudent();
void deleteStudent();
int deleteStudentByIndex(int indexDelete);	
void modifyStudent();
void findStudent(int all);
void sortStudent();
void writeStudent();
void readStudent();

//主函数
int main()
{
	readStudent();
	while(1)
	{
		int choice;
		menu();
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
				addStudent();
				break;
			case 2:
				findStudent(1); //直接显示所有学生
				deleteStudent();
				break;
			case 3:
				findStudent(1); //直接显示所有学生
				modifyStudent();
				break;
			case 4:
				findStudent(0);
				break;
			case 5:
				findStudent(1); //直接显示所有学生
				sortStudent();
				break;
			case 6:
				writeStudent();
				printf("保存成功!\n");
				break;
			case 0:
				printf("\n\n感谢您的使用!\n\n");
				exit(0);
			default:
				printf("\n输入有误，请重新输入!\n\n");
				system("pause");
				break;
		}
	}
}
